package com.cqtd.role.action;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.cqtd.base.pojo.FooGenericSearch;
import com.cqtd.base.pojo.FooGenericSearchResult;
import com.cqtd.base.service.FooGenericService;
import com.cqtd.role.model.Role;
import com.cqtd.util.FooUtils;
import com.cqtd.util.StaticMethod;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;

@Controller
@RequestMapping("/role/roleAction")
public class RoleAction {
	@SuppressWarnings("unused")
	private static final Logger logger = Logger.getLogger(RoleAction.class);
	@Autowired
	private FooGenericService fooGenericService;

	@RequestMapping("/rolelist")
	@ResponseBody
	public void rolelist(HttpServletResponse response) {

		String hql = "from Role";
		String countHql = "select count(id) " + hql;

		FooGenericSearch fooGenericSearch = new FooGenericSearch();
		fooGenericSearch.setQueryHql(hql);
		fooGenericSearch.setPage(1);
		fooGenericSearch.setRows(15);
		fooGenericSearch.setCountHql(countHql);
		FooGenericSearchResult fooGenericSearchResult = fooGenericService
				.searchAndCount(fooGenericSearch);
		Map<String, Object> map = Maps.newHashMap();
		map.put("datas", fooGenericSearchResult.getResult());
		map.put("total", fooGenericSearchResult.getTotalCount());
		try {
			FooUtils.printJsonObject(response, map);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	@RequestMapping("/editor")
	@ResponseBody
	public void editor(HttpServletResponse response, Role role) {
		fooGenericService.save(role);
		try {
			FooUtils.printJsonObject(response);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	@SuppressWarnings({ "unused", "unchecked" })
	@RequestMapping("/rolelist2")
	@ResponseBody
	public void rolelist2(HttpServletRequest request,
			HttpServletResponse response) {
		List<Role> result = Lists.newArrayList();
		String node = StaticMethod
				.nullObject2String(request.getParameter("id"));
		String hql = "from Role where parentId=:parentId";
		Map<String, Object> params = new HashMap<String, Object>();
		if (node.equals("root") || "".equals(node)) {
			node = "";
		}
		params.put("parentId", node);
		FooGenericSearch fooGenericSearch = new FooGenericSearch();
		result = (List<Role>) fooGenericService.search(fooGenericSearch,params)
				.getResult();
		
		Map<String, Object> map = Maps.newHashMap();
		map.put("models", result);

		try {
			FooUtils.printJsonObject(response, map);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

}
